/*   Copyright (C) 2008  Jeroen v. Schagen & Youri Lima
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;

using Engine.Math;
using Engine.Rendering.Library;

namespace Engine.Rendering
{
    // Skybox
    public class Skybox : IRenderable
    {
        private String file = "";
        private String prefix = "";

        // To be wrapped
        private Microsoft.DirectX.Direct3D.Mesh mesh;
        private Microsoft.DirectX.Direct3D.Material[] materials;
        private Microsoft.DirectX.Direct3D.Texture[] textures;

        public Skybox(String prefix)
        {
            this.prefix = prefix;
            this.file = "data/model/skybox.x";
        }

        public void Init(Microsoft.DirectX.Direct3D.Device device)
        {
            if (file != null && file != String.Empty)
            {
                Microsoft.DirectX.Direct3D.ExtendedMaterial[] materials = null;

                // Initiate mesh
                mesh = Microsoft.DirectX.Direct3D.Mesh.FromFile(file, Microsoft.DirectX.Direct3D.MeshFlags.SystemMemory, device, out materials);

                // Initiate textures and material
                if ((materials != null) && (materials.Length > 0))
                {
                    this.textures = new Microsoft.DirectX.Direct3D.Texture[materials.Length];
                    this.materials = new Microsoft.DirectX.Direct3D.Material[materials.Length];

                    for (int i = 0; i < materials.Length; i++)
                    {
                        this.materials[i] = materials[i].Material3D;
                        this.materials[i].Ambient = this.materials[i].Diffuse;

                        if (materials[i].TextureFilename != null && (materials[i].TextureFilename != String.Empty))
                            this.textures[i] = Microsoft.DirectX.Direct3D.TextureLoader.FromFile(device, "data/texture/" + materials[i].TextureFilename);
                    }
                }
            }

            else
            {
                throw new Exception("No source for the meshfile specified");
            }
        }

        public void Render(Microsoft.DirectX.Direct3D.Device device, Camera camera)
        {
            // Setup matrices
            Microsoft.DirectX.Matrix worldMatrix = DirectX.ToDirectX(Matrix4.FromScale(new Vector3(100, 100, 100)));
            Microsoft.DirectX.Matrix viewMatrix = DirectX.ToDirectX(((Matrix4) camera.GetAdapter(typeof(Matrix4))).Inverse);

            viewMatrix.M41 = 0.0f;
            viewMatrix.M42 = 0.0f;
            viewMatrix.M43 = 0.0f;

            // Transform
            device.Transform.View = viewMatrix;
            device.Transform.World = worldMatrix;

            // Setup rendering
            device.RenderState.ZBufferWriteEnable = false;
            device.RenderState.CullMode = Microsoft.DirectX.Direct3D.Cull.None;

            // Attach materials
            for (int i = 0; i < materials.Length; i++)
            {
                device.Material = materials[i];
                device.SetTexture(0, textures[i]);

                mesh.DrawSubset(i);
            }

            // Restore to default
            device.Transform.View = DirectX.ToDirectX(((Matrix4) camera.GetAdapter(typeof(Matrix4))).Inverse);
            device.RenderState.ZBufferWriteEnable = true;
            device.RenderState.CullMode = Microsoft.DirectX.Direct3D.Cull.CounterClockwise;
        }
    }
}
